package controller

import (
	"net/http"

	"gomvc/app/services"

	"github.com/gin-gonic/gin"
)

var Login = new(loginCtl)

type loginCtl struct{}

type LoginReq struct {
	UserName string `form:"username" binding:"required,min=5,max=30"`
	Password string `form:"password" binding:"required,min=6,max=12"`
	Captcha  string `form:"captcha" binding:"required,min=4,max=6"`
	IdKey    string `form:"idKey" binding:"required"`
}

func (anony *loginCtl) Login(c *gin.Context) {
	var req *LoginReq
	// 获取参数
	err := c.ShouldBind(&req)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": -1,
			"Msg":  err.Error(),
		})
		return
	}
	// 调用登录
	token, err := services.Login.UserLogin(req.UserName, req.Password, c)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": -1,
			"Msg":  err.Error(),
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 0,
		"Msg":  "登录成功",
		"Data": gin.H{
			"token": token,
		},
	})

}

type RegisterReq struct {
	UserName string `form:"username" binding:"required,min=5,max=30"`
	Password string `form:"password" binding:"required,min=6,max=12"`
	Captcha  string `form:"captcha" binding:"required,min=4,max=6"`
	IdKey    string `form:"idKey" binding:"required"`
}

func (anony *loginCtl) Register(c *gin.Context) {

}
